package com.android.fragmentdemo
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.fragmentdemo.adapters.DynamicItem03Adapter
import com.hengfoxwolf.commonbase.base.BaseActivity3
import com.android.fragmentdemo.databeans.DataFactoryIp
import com.android.fragmentdemo.databinding.ActivityPullUpToLoadBinding
import com.android.fragmentdemo.defineviews.DeOverScrollRecyclerView
import com.android.fragmentdemo.defineviews.List01DataItemDecoration
import com.android.fragmentdemo.defineviews.PullToLoadMoreLayout
import com.android.fragmentdemo.util.StepLoadCollectionUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class PullUpToLoadActivity :
    BaseActivity3<ActivityPullUpToLoadBinding>(ActivityPullUpToLoadBinding::inflate),
    PullToLoadMoreLayout.IPullToRefreshCallback {

    private lateinit var mDyAdapter: DynamicItem03Adapter

    override fun init(savedInstanceState: Bundle?) {
        mDyAdapter = DynamicItem03Adapter()
        val dyLayoutManager = LinearLayoutManager(this@PullUpToLoadActivity)
        StepLoadCollectionUtil.init(DataFactoryIp.createStrDataSet(), 15)
        mDyAdapter.itemDataSet = StepLoadCollectionUtil.getInitDataSet()
        mBinding.rvPullToLoad.apply {
            layoutManager = dyLayoutManager
            adapter = mDyAdapter
            addItemDecoration(
                List01DataItemDecoration(
                    this@PullUpToLoadActivity,
                    0F
                ).apply {
                    setDrawLastLine(false)
                })
            initAdapter(mDyAdapter)
            setActionState(DeOverScrollRecyclerView.ActionState.PullToLoadMore)
        }
        mBinding.layoutPullLoadMore.setPullToRefreshCallback(this)
    }

    override fun pullDownToRefresh() {}

    override fun pullUpToLoad() {
        CoroutineScope(Dispatchers.Main).launch {
            delay(2000)
            mBinding.layoutPullLoadMore.resetViewState()
            Log.d(TAG, "override fun pullUpToLoad()")
        }
    }

    companion object {
        const val TAG = "PullUpToLoadActivity"
    }
}